import socket  
import time
import network

ssid='s-night-y'
password='18084821996smy'

wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True)       # activate the interface
            # scan for access points
print (wlan.isconnected() )     # check if the station is connected to an AP
# connect to an AP
connect=True
if(wlan.isconnected()):
        connect=False
while  connect :
    wlan.connect(ssid, password)
    if(wlan.isconnected()):
        connect=False
# 创建UDP socket对象  
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  
  
# 设置广播模式  
#sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)  
target_address = ('192.168.31.193',12345) 
# 准备广播消息  
message = 'Hello, world!'  
try:
    while True:
        sock.sendto(message,target_address)
        time.sleep(1)
        print("send")
except :
    print("erro")